from urllib.request import urlopen
from bs4 import BeautifulSoup
import pymysql.cursors

# url = "http://www.hbust.com.cn/"
url = "http://www.hbpu.edu.cn/"
response = urlopen(url).read().decode("utf-8")

soup = BeautifulSoup(response, "html.parser")

newsList = soup.select(".vdemo a")

for news in newsList:
    print(news['title'],"====>",news['href'])
    #设置数据库连接信息
    connection = pymysql.connect(host='localhost',user='root',password='qwe123',db='pydb',charset='utf8mb4')
    try:
        with connection.cursor() as cursor:
            # sql语句
            sql = "insert into link (name,url) values(%s,%s)"
            cursor.execute(sql,(news['title'],news['href']))
            connection.commit()
    finally:
        connection.close();
